// TcpSocketSingleton.h
#ifndef TCPSOCKET_SINGLETON_H
#define TCPSOCKET_SINGLETON_H

#include <QTcpSocket>

static QString ServerIP = "127.0.0.1";
static quint16 ServerPort = 12345;

class TcpSocketSingleton
{
public:
    static TcpSocketSingleton& instance()
    {
        static TcpSocketSingleton instance; // 懒汉式单例
        return instance;
    }

    QTcpSocket* socket()
    {
        return tcpSocket;
    }

private:
    TcpSocketSingleton()
    {
        tcpSocket = new QTcpSocket();
        tcpSocket->connectToHost(ServerIP, ServerPort);
    }

    ~TcpSocketSingleton()
    {
        delete tcpSocket;
    }

    // 禁止拷贝和赋值
    TcpSocketSingleton(const TcpSocketSingleton&) = delete;
    TcpSocketSingleton& operator=(const TcpSocketSingleton&) = delete;

    QTcpSocket* tcpSocket;
};

#endif // TCPSOCKET_SINGLETON_H

